package com.jason.www.state_pattern.demo2.state;


import com.jason.www.state_pattern.demo2.context.ATMContext;

/**
 * ATM的准备状态
 */
public class ATMReadyState implements IATMState {
    private ATMContext atmContext;//保留状态持有者的引用，以便对其进行操作

    public ATMReadyState(ATMContext atmContext) {
        this.atmContext = atmContext;
    }

    @Override
    public void insertCard() {
        System.out.println("插卡成功");
    }

    @Override
    public void submitPwd() {
        if("123".equals(atmContext.getPassword())){
            System.out.println("密码正确");
        }else{
            System.out.println("密码错误");
        }
    }

    @Override
    public void getCash() {
        if(atmContext.getTotalMoney() == 0 || atmContext.getTakeMoney() > atmContext.getBalance() || atmContext.getTakeMoney() > atmContext.getTotalMoney()){
            atmContext.setBalance(atmContext.getBalance() - atmContext.getTakeMoney());
            atmContext.setTotalMoney(atmContext.getTotalMoney() - atmContext.getTakeMoney());
            //检查ATM里面的现金，切换到NoCash状态
            if(atmContext.getTotalMoney() == 0){
                atmContext.setCurrState(atmContext.getNoCashState());
                System.out.println("ATM已没有现金，发送通知银行");
            }
            System.out.println("取出现金为:" + atmContext.getTakeMoney());
        }else{
            System.out.println("提现失败！ATM没有足够的现金！");
            //弹出卡片
            ejectCard();
        }
    }

    @Override
    public void queryBalance() {
        System.out.println("本账号余额为：" + atmContext.getBalance());
    }

    @Override
    public void ejectCard() {
        System.out.println("银行卡取回成功，请及时取走");
    }
}
